<?php

namespace app\admin\controller;

class Teacher extends Base
{
    //教师列表
    public function teacher()
    {
        $te = model('common/Teacher') -> where('classify',1) -> paginate(10);
        $data = [
            'te' => $te
        ];
        $this -> assign($data);
        return view();
    }

    //教师添加
    public function teacher_add()
    {
        if (request()->isAjax()){
            $data = input('post.');
            $data['photo'] = request()->file('myFile');
            $result = model('common/Teacher') -> teacher_add($data);
            if ($result == 1){
                $this -> success('添加成功！','admin/teacher/teacher');
            } else {
                $this -> error($result);
            }
        }
        return view();
    }

    //教师编辑
    public function teacher_edit()
    {
        if (request()->isAjax()){
            $data = input('post.');
            $data['photos'] = request()->file('myFile');
            $result = model('common/Teacher') -> teacher_edit($data);
            if ($result == 1){
                $this -> success('更新成功！','admin/teacher/teacher');
            } else {
                $this -> error($result);
            }
        }
        $te = model('common/Teacher') -> find(input('id'));
        $data = [
            'te' => $te
        ];
        $this -> assign($data);
        return view();
    }

    //教师删除
    public function teacher_del()
    {
        if (request()->isAjax()){
            $te = model('common/Teacher') -> find(input('post.id'));
            $result = $te -> delete();
            if ($result){
                $this -> success('删除成功！','admin/teacher/teacher');
            } else {
                $this -> error('删除失败！');
            }
        } else {
            return '非法请求！';
        }
    }


    //专业教师列表
    public function teachers()
    {
        $te = model('common/Teacher') -> where('classify',2) -> paginate(10);
        $data = [
            'te' => $te
        ];
        $this -> assign($data);
        return view();
    }

    //专业教师添加
    public function teachers_add()
    {
        if (request()->isAjax()){
            $data = input('post.');
            $data['photo'] = request()->file('myFile');
            $data['classify'] = 2;
            $result = model('common/Teacher') -> teachers_add($data);
            if ($result == 1){
                $this -> success('添加成功！','admin/teacher/teachers');
            } else {
                $this -> error($result);
            }
        }
        return view();
    }

    //专业教师编辑
    public function teachers_edit()
    {
        if (request()->isAjax()){
            $data = input('post.');
            $data['photos'] = request()->file('myFile');
            $result = model('common/Teacher') -> teachers_edit($data);
            if ($result == 1){
                $this -> success('更新成功！','admin/teacher/teachers');
            } else {
                $this -> error($result);
            }
        }
        $te = model('common/Teacher') -> find(input('id'));
        $data = [
            'te' => $te
        ];
        $this -> assign($data);
        return view();
    }

    //专业教师删除
    public function teachers_del()
    {
        if (request()->isAjax()){
            $te = model('common/Teacher') -> find(input('post.id'));
            $result = $te -> delete();
            if ($result){
                $this -> success('删除成功！','admin/teacher/teachers');
            } else {
                $this -> error('删除失败！');
            }
        } else {
            return '非法请求！';
        }
    }


}
